// SculleryServer.cpp
//
// Copyright (c) 2011 LRUnit, LLC All Rights Reserved.
// Author : Kim McKinley
//
// This product includes software developed by :
//  LRUnit, LLC
//
// This software is under the terms of the MIT License :
/*
  Permission is hereby granted, free of charge, to any person obtaining a copy
  of this software and associated documentation files (the "Software"), to deal
  in the Software without restriction, including without limitation the rights
  to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  copies of the Software, and to permit persons to whom the Software is
  furnished to do so, subject to the following conditions:

  The above copyright notice and this permission notice shall be included in
  all copies or substantial portions of the Software.

  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  THE SOFTWARE.
*/

#include "CDN_APP_SculleryServer.h"

// **********************************************************************
/// Server entry point
int main( int argc, char** argv )
{
  // Server application
  cdn::app::SculleryServer app;

  // Say Hi!
  app.Welcome();

  // Server configuration
  std::string ConfigFile;
  cdn::app::Configuration cfg;

  // First parameter is the configuration file - if given
  if ( argc == 2 )
    ConfigFile = argv[1];
  else // display a usage summary if no default config file found
  {
      std::ifstream defaultfilestr(DEFAULT_CONFIG_FILE, std::fstream::in);
      if ( !defaultfilestr )
        app.Usage();
  }

  // Load the configuration
  cfg.LoadConfiguration(ConfigFile);

  // Insure have a place for the web pages and SQLite databases to live
  app.CreateDirectory(gConfig["DEFAULT"]["DATABASE_DIRECTORY"]);
  app.CreateDirectory(gConfig["DEFAULT"]["WEB_DIRECTORY"]);

  // Create and startup the TcpIp Acceptor to listen for requests
  cdn::net::tcp::Acceptor acceptor;
  os::Thread( acceptor.Start, &acceptor );

  // Give a second to display Acceptor start messages or errors if there are issues
  Sleep(1000);

  // Let user know how to get to the menu
  std::cout << std::endl << "Press Enter for a menu." << std::endl;

  // Run console command handler until shutdown is requested
  while ( true )
  {
    std::cout << std::endl;

    // Process user console commands - returns on '.bye' command
    app.CommandHandler();

    // Is a shutdown confirmation wanted? If not just shutdown
    if ( toupper(gConfig["DEFAULT"]["CONFIRM_BEFORE_SHUTDOWN"][0]) != 'Y' )
      break;

    // Now get shutdown confirmation from user
    char c;
    std::cout << std::endl << gConfig["DEFAULT"]["PROMPT"] << "Press Enter or 'Y' to confirm.  Shutdown (Y)? ";
    std::cin.get(c);
    if (c == '\n' || toupper(c) == 'Y' )
      break;

    // I really wanted to shutdown - but guess user decided not to
    std::cin.ignore();
    std::cout << std::endl << std::endl << gConfig["DEFAULT"]["PROMPT"] << "Continuing to accept requests..."  << std::endl;
  }

  // Shutdown the acceptor that is listening on the server port
  acceptor.Stop();

  // Give a little time for user to read messages before the console window disappears
  std::cout  << std::endl << "Shutdown complete." << std::endl;
  Sleep(2000);

  // Cya l8r - i'm outta here!
  return 0;
}


